//
//  XMGMoreVc.m
//  weibo
//
//  Created by Apple on 16/7/5.
//  Copyright © 2016年 DP. All rights reserved.
//

#import "XMGMoreVc.h"
#import "XMGFoundCell.h"
#import <MJExtension.h>
#import "XMGFoundItem.h"
#import "XMGFoundIGroupItem.h"


@interface XMGMoreVc ()

//channel的数据源
@property(nonatomic, strong)NSArray *channelArray;
@end

//定义一个标识符
static NSString *ID = @"found";

@implementation XMGMoreVc

//懒加载数据
-(NSArray *)channelArray
{
    if (_channelArray == nil) {
//    告诉MJExtension这个框架XMGFoundIGroupItem的group数组属性中装的是XMGFoundItem这种类型的对象
        [XMGFoundIGroupItem mj_setupObjectClassInArray:^NSDictionary *{
            return @{@"group" : [XMGFoundItem class]};
        }];
        
        _channelArray = [XMGFoundIGroupItem mj_objectArrayWithFilename:@"allChannel.plist"];

    }
    return _channelArray;

}


- (void)viewDidLoad {
    [super viewDidLoad];
//    设置导航条的title
    self.navigationItem.title = @"全部频道";
//    让tableView不具有弹簧效果
    self.tableView.bounces = NO;
    
    //    设置导航条左返回键
    UIButton *btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:@"navigationbar_back"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] forState:UIControlStateHighlighted];
    [btn sizeToFit];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    //    监控按钮点击
    [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    
    //    设置导航条右编辑键
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setTitle:@"编辑" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    [btn setTitle:@"编辑" forState:UIControlStateHighlighted];
    [btn1 setTitleColor:[UIColor colorWithRed:1.0 green:0.5 blue:0.0 alpha:0.4] forState:UIControlStateHighlighted];
    
    [btn1 sizeToFit];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn1];
    [btn1 addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
    
//    注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGFoundCell class]) bundle:nil] forCellReuseIdentifier:ID];
    
//    设置cell的高度
    self.tableView.rowHeight = 60;
//    self.tableView.sectionHeaderHeight = 30;
    
 
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.channelArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    XMGFoundIGroupItem  *foundItem = self.channelArray[section];
    return foundItem.group.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    创建cell
    XMGFoundCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
//    给cell赋值
    XMGFoundIGroupItem *groupItem = self.channelArray[indexPath.section];
    XMGFoundItem *foundItem = groupItem.group[indexPath.row];
    cell.Foundtem = foundItem;

    return cell;
}

//设置表头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section ==0) {
        return @"已添加的频道";
    }else
    {
    return @"未添加的频道";
    }
}
//设置表头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 40;

}




//当view即将显示的时候调用
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)edit:(UIButton *)btn{
//    设置点击按钮后文字颜色改变
    if ([btn.titleLabel.text isEqualToString:@"编辑"]) {
        [btn setTitle:@"完成" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [btn setTitle:@"完成" forState:UIControlStateHighlighted];
        [btn setTitleColor:[UIColor colorWithRed:1.0 green:0.5 blue:0.0 alpha:0.4] forState:UIControlStateHighlighted];
        
        
        
        
        
    }else if([btn.titleLabel.text isEqualToString:@"完成"]) {
        [btn setTitle:@"编辑" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [btn setTitle:@"编辑" forState:UIControlStateHighlighted];
        [btn setTitleColor:[UIColor colorWithRed:1.0 green:0.5 blue:0.0 alpha:0.4] forState:UIControlStateHighlighted];
        
        
    }
}



@end
